package com.joint.cloud.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author lilengyi
 * @date 2020/1/20 15:04
 */
public class TimeUtil {
    public static final String dateFormat = "yyyyMMddHHmmss";

    public static String toTimeString(Date time) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        return df.format(time);
    }

    public static Date fromTimeString(String timeStr) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        try {
            return df.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 向下取整小时
     * @param time
     * @return
     */
    public static Date toHourRoundDown(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String hour = df.format(time);
        hour = hour.substring(0, 10).concat("0000");
        try {
            return df.parse(hour);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 向上取整小时
     * @param time
     * @return
     */
    public static Date toHourRoundUp(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String timeStr = df.format(time);
        if (!"0000".equals(timeStr.substring(10, 14))){
            Calendar c = Calendar.getInstance();
            c.setTime(toHourRoundDown(time));
            c.add(Calendar.HOUR_OF_DAY, 1);// +1天
            return c.getTime();
        } else {
            return time;
        }
    }

    /**
     * 取下一小时
     * @param time
     * @return
     */
    public static Date toNextHour(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.HOUR_OF_DAY, 1);// +1天
        return c.getTime();
    }

    /**
     * 取前一小时
     * @param time
     * @return
     */
    public static Date toPreHour(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.HOUR_OF_DAY, -1);// +1天
        return c.getTime();
    }

    /**
     * 向下取整天数，设为当天0点
     * @param time
     * @return
     */
    public static Date toDayRoundDown(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String dayTime = df.format(time);
        dayTime = dayTime.substring(0, 8).concat("000000");
        try {
            return df.parse(dayTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 向上取整天数，如果时间不为整天设为第二天0点
     * @param time
     * @return
     */
    public static Date toDayRoundUp(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String timeStr = df.format(time);
        if (!"000000".equals(timeStr.substring(8, 14))){
            Calendar c = Calendar.getInstance();
            c.setTime(toDayRoundDown(time));
            c.add(Calendar.DAY_OF_MONTH, 1);// +1天
            return c.getTime();
        } else {
            return time;
        }
    }

    /**
     * 取下一天日期
     * @param time
     * @return
     */
    public static Date toNextDay(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.DAY_OF_MONTH, 1);// +1天
        return c.getTime();
    }

    /**
     * 取前一天日期
     * @param time
     * @return
     */
    public static Date toPreDay(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.DAY_OF_MONTH, -1);// +1天
        return c.getTime();
    }

    /**
     * 向下取整月数，设为当月1日0点
     * @param time
     * @return
     */
    public static Date toMonthRoundDown(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String monthTime = df.format(time);
        monthTime = monthTime.substring(0, 6).concat("01000000");
        try {
            return df.parse(monthTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 向上取整天数，如果时间不为整月设为下个月1号0点
     * @param time
     * @return
     */
    public static Date toMonthRoundUp(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String timeStr = df.format(time);
        if (!"01000000".equals(timeStr.substring(6, 14))){
            return toNextMonth(toMonthRoundDown(time));
        } else {
            return time;
        }

    }

    /**
     * 取下一月日期
     * @param time
     * @return
     */
    public static Date toNextMonth(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.MONTH, 1);// +1月
        return c.getTime();
    }

    /**
     * 取上一月日期
     * @param time
     * @return
     */
    public static Date toPreMonth(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.MONTH, -1);// -1月
        return c.getTime();
    }

    /**
     * 向下取整年，设为当年1日0点
     * @param time
     * @return
     */
    public static Date toYearRoundDown(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String monthTime = df.format(time);
        monthTime = monthTime.substring(0, 4).concat("0101000000");
        try {
            return df.parse(monthTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 向下取整年，设为当年1日0点
     * @param time
     * @return
     */
    public static Date toYearRoundUp(Date time){
        DateFormat df = new SimpleDateFormat(dateFormat);
        String timeStr = df.format(time);
        if (!"0101000000".equals(timeStr.substring(4, 14))){
            return toNextYear(toYearRoundDown(time));
        } else {
            return time;
        }
    }

    /**
     * 取下一年日期
     * @param time
     * @return
     */
    public static Date toNextYear(Date time){
        Calendar c = Calendar.getInstance();
        c.setTime(time);
        c.add(Calendar.YEAR, 1);// +1月
        return c.getTime();
    }
}
